from django.http import HttpResponse
from django.shortcuts import render, redirect


# Create your views here.

# 默认有参request
def index(request):
    # 返回浏览器请求
    return HttpResponse("Hello, world. You're at the")


def user_list(request):
    # 返回html模板  在根目录目录下的templates中寻找模板
    return render(request, 'user_list.html')


def user_add(request):
    return render(request, 'user_add.html')


def tpl(request):
    name = "耶耶耶"
    roles = ["111", "222", "333"]
    people = {"name": "yyy", "roles": "ceo", "age": 20}
    people_list = [
        {"name": "yyy", "roles": "ceo", "age": 20},
        {"name": "xxx", "roles": "aeo", "age": 21},
        {"name": "kkk", "roles": "peo", "age": 22}
    ]
    return render(request, 'tpl.html', {"n1": name, "n2": roles, "n3": people, "n4": people_list})


def something(request):
    # req是一个对象，封装了用户发送过来的所有请求相关数据

    # 1.获取请求方式  http://localhost:8000/something
    # print(request.method)

    # 2.在url上传递值  http://localhost:8000/something/?n1=123&n2=999
    # print(request.GET)
    # 响应 3.响应请求者的请求
    # return HttpResponse("返回浏览器，响应！")

    # 响应 4.读取html的内容+渲染，返还给浏览器字符串
    # return render(request, "something.html")

    # 响应 5.重定向
    # return redirect("http://www.baidu.com")
    return '11'


def login(request):
    # get 请求，url所请求的
    # post请求，内部的数据传输
    error = ''
    # 在url中输入地址（get请求），则返回登陆页面
    if request.method == 'GET':
        return render(request, 'login.html', {"error": error})

    # 若是post请求，则获取数据(返回一个字典{"name":"value"})
    print(request.POST)
    # 获取数据
    username = request.POST.get("user")
    password = request.POST.get("pwd")

    if username == 'admin' and password == '123':
        return redirect('https://www.chinaunicom.com/')

    error = '登陆失败'
    return render(request, 'login.html', {"error": error})


'''
引用orm的两种方式
1.表名.
from app01.models import UserInfo
UserInfo.xxx
2.
import app01.models
models.UserInfo.xxx
'''
from app01.models import UserInfo


def db(request):
    ######  插入数据  ######
    UserInfo.objects.create(name='殷琪尧', password='123456', age=10, size=50)
    UserInfo.objects.create(name='yyy', password='12345622', age=15)
    UserInfo.objects.create(name='琪尧', password='123222456', age=20)

    #####  查询数据  #####
    # 返回符合条件的所有数据 [数据对象，数据对象，数据对象] QuerySet类型
    data_list = UserInfo.objects.filter(id=1)
    for data in data_list:
        print(data.name, data.age, data.size)

    # 返回所有数据
    data_list2 = UserInfo.objects.all()
    for data1 in data_list2:
        print(data1.name, data1.age, data1.size)

    # 返回符合条件的第一条数据
    data_list3 = UserInfo.objects.filter(size=2).first()
    print(data_list3.name, data_list3.age, data_list3.size)

    #  data_list均为[数据对象，数据对象，数据对象] QuerySet类型

    # ######  删除数据  #####
    # UserInfo.objects.filter(id=3).delete()
    # UserInfo.objects.all().delete()

    ##### 更新数据  #####
    UserInfo.objects.filter(id=3).update(name="火麒麟")
    UserInfo.objects.all().update(password="999999")

    return HttpResponse("数据库操作成功")


def info_list(request):
    # 1.获取用户信息
    info_list = UserInfo.objects.all()
    # 2.传入html渲染
    return render(request, 'info.html', {'info_list': info_list})


def info_add(request):
    if request.method == ('GET'):
        return render(request, 'info_add.html')
    name = request.POST.get('name')
    pwd = request.POST.get('pwd')
    age = request.POST.get('age')
    UserInfo.objects.create(name=name, password=pwd, age=age)
    return redirect("/info/list/")
